package com.qf.staff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.base.exception.BaseException;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.common.db.utils.FileUploadUtils;
import com.qf.common.db.utils.PageCommonBeanUtils;
import com.qf.staff.common.qo.GoodsQo;
import com.qf.staff.common.vo.GoodsVo;
import com.qf.staff.mapper.GoodsMapper;
import com.qf.staff.pojo.Goods;
import com.qf.staff.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {
    @Resource
    GoodsMapper goodsMapper;
    @Resource
    private FileUploadUtils fileUploadUtils;

    @Override
    public IPage<GoodsVo> list(int page, int size, GoodsQo goodsQo) {
        try {
            QueryWrapper<Goods> qw = new QueryWrapper<>();
            if (goodsQo.getGoodsName() != null) {
                qw.eq("goods_name", goodsQo.getGoodsName());
            }
            return PageCommonBeanUtils.copyPage(goodsMapper.selectPage(new Page<>(page, size), qw),
                    new Page<>(), GoodsVo::new);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
    }

    @Override
    public int remove(GoodsQo goodsQo) {
        try {
            Goods goods = new Goods();
            BeanUtils.copyProperties(goodsQo, goods);
            return goodsMapper.updateByStatus(goods);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }

    @Transactional
    @Override
    public int removeAll(List<Integer> ids, int status) {
        try {
            int num = goodsMapper.updateByAllStatus(ids, status);
            if (num < ids.size()) {
                throw new ServiceException(ResultCode.DELETE_ERROR);
            }
            return num;
        } catch (Exception e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }


    @Override
    public int modify(GoodsQo goodsQo) {
        try {
            Goods goods = new Goods();
            BeanUtils.copyProperties(goodsQo, goods);
            return goodsMapper.updateById(goods);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
    }

    @Override
    public int add(GoodsQo goodsQo, MultipartFile file) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsQo, goods);
        try {
            String s = fileUploadUtils.uploadImage(file);
            if (!ObjectUtils.isEmpty(s)) {
                goods.setGoodsImg(s);
                return goodsMapper.insert(goods);
            } else {
                throw new ServiceException(ResultCode.IMG_NOT_EXIST);
            }
        } catch (RuntimeException e) {
            throw new BaseException(ResultCode.IMG_NOT_EXIST);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }
}
